package com.ldc.composetest.screen.article_screen

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.ldc.commonlibs.uts.ToastUtils
import com.ldc.composetest.compose_screen.LoadingScreen
import com.ldc.composetest.compose_screen.TitleBar
import com.ldc.composetest.viewmodel.ComposeViewModel

@Composable
fun ArticleListScreen(
    navController: NavController = rememberNavController(),
    onFinish: () -> Unit
) {
    val context = LocalContext.current
    val currPage by remember {
        mutableStateOf(1)
    }
    val viewModel = viewModel(modelClass = ComposeViewModel::class.java)
    viewModel.getArticleList(currPage)
    val articleListData = viewModel.articleModel.value
    val showLoading = viewModel.showLoading.value
    Scaffold(topBar = {
        TitleBar(title = "文章列表", onFinish) {
            viewModel.getArticleList(currPage + 1)
        }
    }) {

        LazyColumn(modifier = Modifier.fillMaxSize()) {
            itemsIndexed(articleListData) { _, item ->
                ArticleItemScreen(article = item, okClick = {
                    ToastUtils.showShort(it.title)
                })
            }
        }
        if (showLoading) {
            LoadingScreen()
        }

    }
}